package com.leji.song.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.*;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.*;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;
import com.leji.song.R;
import time.JudgeDate;
import time.ScreenInfo;
import time.WheelMain;

/**
 * 日期时间选择控件
 * @author 大漠
 */
public class DateTimePickerDialog implements  OnDateChangedListener,OnTimeChangedListener{
    private DatePicker datePicker;
    private TimePicker timePicker;
    private AlertDialog ad;
    private String dateTime;
    private String initDateTime;
    private Activity activity;

    public interface GetTimeCallback{
       void onGetTime(String time);
    }
    /**
     * 日期时间弹出选择框构
     * @param activity：调用的父activity
     */
    public DateTimePickerDialog(Activity activity)
    {
        this.activity = activity;
    }

    public void init(DatePicker datePicker,TimePicker timePicker)
    {
        Calendar calendar= Calendar.getInstance();
        initDateTime=calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+
                calendar.get(Calendar.DAY_OF_MONTH)+" "+
                calendar.get(Calendar.HOUR_OF_DAY)+":"+
                calendar.get(Calendar.MINUTE)+
                calendar.get(Calendar.SECOND);
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH), this);
        timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
        timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateTime=sdf.format(calendar.getTime());
    }

    /**
     * 弹出日期时间选择框
     * @param type: 0为日期时间类型:yyyy-MM-dd HH:mm:ss
     *         1为日期类型:yyyy-MM-dd
     *         2为时间类型:HH:mm:ss
     * @return
     */
    public AlertDialog dateTimePicKDialog(final GetTimeCallback getTimeCallback, int type)
    {
        Calendar c = Calendar.getInstance();
        switch (type) {
            case 1:
                new DatePickerDialog(activity,
                        new DatePickerDialog.OnDateSetListener() {
                            public void onDateSet(DatePicker datePicker, int year, int monthOfYear,
                                                  int dayOfMonth) {
                                Calendar calendar = Calendar.getInstance();
                                calendar.set(datePicker.getYear(), datePicker.getMonth(),
                                        datePicker.getDayOfMonth());
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                                dateTime=sdf.format(calendar.getTime());

                                getTimeCallback.onGetTime(dateTime);
                            }
                        },
                        c.get(Calendar.YEAR),
                        c.get(Calendar.MONTH),
                        c.get(Calendar.DATE)).show();
                return null;
            case 2:
                new TimePickerDialog(activity,
                        new TimePickerDialog.OnTimeSetListener() {
                            public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                                Calendar calendar = Calendar.getInstance();
                                calendar.set(Calendar.YEAR, Calendar.MONTH,
                                        Calendar.DAY_OF_MONTH, timePicker.getCurrentHour(),
                                        timePicker.getCurrentMinute());
                                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                                dateTime=sdf.format(calendar.getTime());
                                getTimeCallback.onGetTime(dateTime);
                            }
                        },
                        c.get(Calendar.HOUR_OF_DAY),
                        c.get(Calendar.MINUTE),
                        true).show();
                return null;
            case 3:

                final Dialog dialog = new Dialog(activity, R.style.MyCustomDialog);
                dialog.setContentView(R.layout.date_time_picker_dialog3);

                ScreenInfo screenInfo = new ScreenInfo(activity);
                View timepickerview = dialog.findViewById(R.id.timepickerview);
                final WheelMain wheelMain = new WheelMain(timepickerview);
                wheelMain.screenheight = screenInfo.getHeight();
                wheelMain.setView(timepickerview);
                wheelMain.initDateTimePicker();

                dialog.findViewById(R.id.cancle).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        getTimeCallback.onGetTime(wheelMain.getTimeString());
                    }
                });

                dialog.show();
                return ad;
//            default:
//
//                final Dialog dialog = new Dialog(activity, R.style.MyCustomDialog);
//                dialog.setContentView(R.layout.date_time_picker_dialog);
//
//                datePicker = (DatePicker) dialog.findViewById(R.id.datepicker);
//                timePicker = (TimePicker) dialog.findViewById(R.id.timepicker);
//                init(datePicker,timePicker);
//                timePicker.setIs24HourView(true);
//                timePicker.setOnTimeChangedListener(this);
//
//                dialog.findViewById(R.id.cancle).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.dismiss();
//                    }
//                });
//
//                dialog.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.dismiss();
//                        getTimeCallback.onGetTime(dateTime);
//                    }
//                });
//
//                dialog.show();
//                return ad;
        }

        return null;
    }

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
    {
        onDateChanged(null, 0, 0, 0);
    }
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.set(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth(), timePicker.getCurrentHour(),
                timePicker.getCurrentMinute());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateTime=sdf.format(calendar.getTime());
//        ad.setTitle(dateTime);
    }

}